// index.js
import request from '../../utils/request'

Page({
  data: {
    swiperImg:[],
    recommend:[],
    storeList:[],
    city:'',
    courseList:[]
  },
  toStoreInfo(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/store-info/store-info?id='+id
    })
  },
  getCourseList(){
    request({
      url:'/course/list'
    }).then(res=>{
     if(res.code === 200){
       const {city,courseList} = res.data[0]
       this.setData({
         city,
         courseList
       })
     }
    }).catch(err=>{
      console.log(err);
    })
  },
  getSwiper(){
    request({
      url:'/activity/list',
    }).then(res=>{
      if(res.code === 200){
        var recommend = this.data.recommend
        var swiperImg = this.data.swiperImg
        for(let i=0;i<res.data.length;i++){
          if(res.data[i].types === 1){
            recommend.push(res.data[i].cover)
          }else if(res.data[i].types === 0){
            swiperImg.push(res.data[i].cover)
          }
        }
        this.setData({
          recommend:recommend,
          swiperImg:swiperImg
        })
      }else{
        console.log(res.message);
      }
    }).catch(err=>{
      console.log(err);
    })
  },
  getStoreList(){
    request({
      url:'/store/list' 
    }).then(res=>{
      if(res.code === 200){
        const {data} = res
        this.setData({
          storeList:data
        })

      }
    }).catch(err=>{
      console.log(err);
    })
  },
  onLoad(){
    this.getCourseList()
    this.getSwiper()
    this.getStoreList()
    console.log(this.data.recommend);
  }
});

